window.addEventListener("load", function () {
	// 轮播图自动播放标记
	autoPlayTimer = 0;
	// 找对象
	var divContainer = this.document.querySelector("div.carousel>div.container");
	var leftButton = this.document.querySelector("div.arrow-left");
	var rightButton = this.document.querySelector("div.arrow-right");
	var dotArray = this.document.querySelectorAll("div.dots>a");
	var imgArray = this.document.querySelectorAll("div.carousel img");
	// 合并成一个数字
	var currentNumber = 0;
	// 函数节流
	var lock = false;
	// 左右按钮点击事件
	leftButton.addEventListener("click", function () {
		if (lock) {
			return;
		}
		lock = true;
		dotHightLightAndImgShow(--currentNumber);
		setTimeout(() => {
			lock = false;
		}, 1000);
	});
	rightButton.addEventListener("click", function () {
		if (lock) {
			return;
		}
		lock = true;
		dotHightLightAndImgShow(++currentNumber);
		setTimeout(() => {
			lock = false;
		}, 1000);
	});
	// 给小圆点添加点击事件
	for (var i = 0; i < dotArray.length; i++) {
		dotArray[i].onclick = function () {
			dotHightLightAndImgShow((currentNumber = this.dataset.index));
		};
	}
	// 小圆点高亮+图片切换
	function dotHightLightAndImgShow(index) {
		if (index == -1) {
			index = currentNumber = 4;
		}
		if (index == 5) {
			index = currentNumber = 0;
		}
		for (var i = 0; i < dotArray.length; i++) {
			dotArray[i].classList.remove("active");
			imgArray[i].classList.remove("img-active");
		}
		dotArray[index].classList.add("active");
		imgArray[index].classList.add("img-active");
	}
	// 轮播图自动播放函数
	function autoPlay() {
		autoPlayTimer = setInterval(() => {
			rightButton.click();
		}, 1000);
	}
	// 开始轮播图播放
	autoPlay();
	// 绑定鼠标移入移出事件
	divContainer.onmouseenter = function () {
		clearInterval(autoPlayTimer);
	};
	divContainer.onmouseleave = function () {
		autoPlay();
	};
});
